package cn.cheqi.recovery.api.controller;


import cn.cheqi.recovery.api.dto.DataGridView;
import cn.cheqi.recovery.api.entity.Building;
import cn.cheqi.recovery.api.entity.User;
import cn.cheqi.recovery.api.service.BuildingService;
import cn.cheqi.recovery.api.vo.BuildingVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  api接口楼栋前端控制器
 * </p>
 *
 * @author Robin
 * @since 2020-02-23
 */
@RestController("apiBuildingController")
@RequestMapping("/api/building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    /**
     * api接口查出所有楼栋信息
     * @param buildingVO
     * @return
     */
    @RequestMapping("/loadAllBuildings")
    public DataGridView loadAllBuildings(BuildingVO buildingVO){
        IPage<Building> page = new Page<>(buildingVO.getPage(),buildingVO.getLimit());
        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        //获取模糊查询的条件
        //楼栋编号
        Long buildingId = buildingVO.getBuildingId();
        //小区名称
        String communityName = buildingVO.getCommunityName();
        //详细地址
        String detailAddress = buildingVO.getDetailAddress();
        if (buildingId != null){
            wrapper.like("building_id",buildingId);
        }
        if (!StringUtils.isBlank(communityName)){
            String newInfo = communityName.replace(" ", "");
            wrapper.like(!StringUtils.isBlank(newInfo),"community_name",newInfo);
        }
        if (detailAddress != null){
            wrapper.like("detail_address",detailAddress);
        }
        wrapper.orderByDesc("building_id");
        buildingService.page(page,wrapper);
        //回显的DTO对象
        DataGridView dataGridView = new DataGridView();
        dataGridView.setCode(0);
        dataGridView.setMsg("success");
        //数据集合
        Map<Object,Object> map = new HashMap<>();
        //返回数据条数
        map.put("count",page.getRecords().size());
        //当前页
        map.put("page",page.getCurrent());
        //总页数
        map.put("zpage",page.getPages());
        //数据
        map.put("list",page.getRecords());
        dataGridView.setData(map);
        return dataGridView;
    }


    /**
     * 通过楼栋ID获取楼栋信息
     * @param buildingVO
     * @return
     */
    @PostMapping("/getBuildingById")
    public DataGridView getBuildingById(BuildingVO buildingVO){
        Building building = buildingService.getById(buildingVO.getBuildingId());
        if (building == null){
            return new DataGridView(1,"该楼栋不存在","");
        }else {
            return new DataGridView(0,"",building);
        }
    }


    /**
     * api接口添加楼栋信息
     * @param buildingVO
     * @return
     */
    @PostMapping("/addBuilding")
    @Transactional
    public DataGridView addBuildingInfo(BuildingVO buildingVO){
        try {
            buildingService.save(buildingVO);
            return new DataGridView(0,"添加成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGridView(1,"添加失败",null);
        }
    }

    /**
     * api接口根据楼栋ID修改楼栋信息
     * @param buildingVO
     * @return
     */
    @PostMapping("/editBuilding")
    @Transactional
    public DataGridView editBuildingInfoById(BuildingVO buildingVO){
        //判断楼栋信息是否存在
        Building building = buildingService.getById(buildingVO.getBuildingId());
        if (building == null){
            return new DataGridView(1,"您所要编辑的楼栋不存在","");
        }
        //判断编辑信息不能为空
        if (buildingVO.getBuildingFloor()==null&&buildingVO.getBuildingInfo()==null
                &&buildingVO.getCommunityId()==null&&buildingVO.getCommunityName()==null&&buildingVO.getDetailAddress()==null){
            return new DataGridView(1,"你所编辑的信息不能为空","");
        }
        try {
            buildingService.updateById(buildingVO);
            return new DataGridView(0,"修改成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGridView(1,"修改失败",null);
        }
    }

    /**
     * api接口通过楼栋ID删除楼栋信息
     * @param buildingVO
     * @return
     */
    @PostMapping("/deleteBuildingById")
    @Transactional
    public DataGridView deleteBuildingById(BuildingVO buildingVO){
        Building building = buildingService.getById(buildingVO.getBuildingId());
        if (building == null){
            return new DataGridView(1,"您所要删除的楼栋信息不存在","");
        }
        try {
            buildingService.removeById(buildingVO.getBuildingId());
            return new DataGridView(0,"删除成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGridView(1,"删除失败",null);
        }
    }

    /**
     * 通过每栋楼的ID查出所有单元的详情
     * @param buildingId
     * @return
     */
    @PostMapping("/findEveryBuildingInfo")
    @Transactional
    public DataGridView findEveryBuildingInfo(Integer buildingId){
        Building building = buildingService.getById(buildingId);
        if (building == null){
            return new DataGridView(1,"您想要查找的楼栋信息不存在","");
        }
        //如果查找的楼栋存在
        String buildingFloor = building.getBuildingFloor();
        String[] everyDoors = buildingFloor.split(",");
        DataGridView dataGridView = new DataGridView();
        dataGridView.setCode(0);
        dataGridView.setMsg("");
        ArrayList<Object> list = new ArrayList<>();
        for (String everyDoor : everyDoors) {
            Map<Object,Object> map = new HashMap<>();
            //楼栋ID
            map.put("buildingId",building.getBuildingId());
            //哪一栋
            map.put("buildingInfo",building.getBuildingInfo());
            //哪户人家
            map.put("buildingDoor",everyDoor);
            list.add(map);
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("list",list);
        dataGridView.setData(map);
        return dataGridView;
    }

    /**
     * 根据楼栋ID和哪一户人家删除该户,然后再次返回该楼栋的单元详情
     * @param buildingId
     * @param buildingDoor
     * @return
     */
    @PostMapping("/deleteWhichUnit")
    @Transactional
    public DataGridView deleteWhichUnit(Integer buildingId,String buildingDoor){
        Building building = buildingService.getById(buildingId);
        //如果楼栋信息不存在
        if (building == null){
            return new DataGridView(1,"你想要删除的楼栋信息不存在","");
        }
        //如果存在
        //该楼栋的所有单元信息
        String buildingFloor = building.getBuildingFloor();
        String newFloorInfo = "";
        //判断要删除的这户人家是否为楼栋信息的结尾
        if (buildingFloor.endsWith(buildingDoor)){
            newFloorInfo = buildingFloor.replace("," + buildingDoor, "");
        }else {
            //如果是其他位置
            newFloorInfo = buildingFloor.replace(buildingDoor + ",", "");
        }
        //更新楼栋的单元信息
        UpdateWrapper<Building> updateWrapper = new UpdateWrapper<>();
        Building buildingInfo = new Building();
        buildingInfo.setBuildingFloor(newFloorInfo);
        updateWrapper.eq("building_id",building.getBuildingId());
        buildingService.update(buildingInfo,updateWrapper);

        //重新返回楼栋单元信息的详情
        Building afterUpdateBuilding = buildingService.getById(buildingId);
        String afterBuildingFloor = afterUpdateBuilding.getBuildingFloor();
        String[] everyDoors = afterBuildingFloor.split(",");
        DataGridView dataGridView = new DataGridView();
        dataGridView.setCode(0);
        dataGridView.setMsg("删除成功");
        ArrayList<Object> list = new ArrayList<>();
        for (String everyDoor : everyDoors) {
            Map<Object,Object> map = new HashMap<>();
            //楼栋ID
            map.put("buildingId",afterUpdateBuilding.getBuildingId());
            //哪一栋
            map.put("buildingInfo",afterUpdateBuilding.getBuildingInfo());
            //哪户人家
            map.put("buildingDoor",everyDoor);
            list.add(map);
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("list",list);
        dataGridView.setData(map);
        return dataGridView;
    }


    /**
     * 根据楼栋ID和输入的房间信息添加房间号
     * @param buildingId 楼栋ID
     * @param buildingDoor 哪一户
     * @return
     */
    @PostMapping("/addWhichBuildingDoor")
    @Transactional
    public DataGridView addWhichBuildingDoor(Integer buildingId,String buildingDoor){
        Building building = buildingService.getById(buildingId);
        if (building == null){
            return new DataGridView(1,"该楼栋信息不存在","");
        }
        //如果要添加的房间为空
        if (buildingDoor == "" || buildingDoor.equals(" ")){
            return new DataGridView(1,"您所要添加的信息为空","");
        }
        //如果存在
        String buildingFloor = building.getBuildingFloor();
        if (buildingFloor.equals("")){
            buildingFloor = buildingDoor;
        }else {
            buildingFloor = buildingFloor + "," + buildingDoor;
        }
        UpdateWrapper<Building> updateWrapper = new UpdateWrapper<>();
        Building afterBuilding = new Building();
        afterBuilding.setBuildingFloor(buildingFloor);
        updateWrapper.eq("building_id",building.getBuildingId());
        buildingService.update(afterBuilding,updateWrapper);
        return new DataGridView(0,"添加成功","");
    }
}

